<?php
class Router {
	
	public function getLanguage($language){
		require_once './lang/'.$language.'.php';
	}
	
	public function getVariableParser($request, array $postVars){
		/**
		 * parse the page request and other GET variables
		 */
		$parsed = explode('&',$request);
		
		/**
		 * the page is the first element
		 */
		$page = htmlentities(array_shift($parsed));
		
		/**
		 * the rest of the array are get statements, parse them out
		 */
		$getVars = array();
		foreach($parsed as $argument){
			/**
			 * split GET vars along '=' symbol to separate variable, values
			 */
			list($variable, $value) = explode('=', $argument);
			$getVars[$variable] = urldecode($value);
		}
		
		/**
		 * compute the path to the file
		 */
		$target = SERVER_ROOT . "/" . $page . "/" . basename("controllers") . "/" . $page . ".php";
		
		/**
		 * load controller
		 */
		$this->controllerLoader($target, $page, $getVars, $postVars);
	}
	
	/**
	 * controllerLoader()
	 * 
	 * This function load controller.
	 * 
	 * @param $page
	 * @param $target
	 */
	public function controllerLoader($target, $page, array $getVars, array $postVars){
		
		try {

			if(file_exists($target)){
				
				include_once($target);
				
				/**
				 * modify page to fit naming convention
				 */
				$page = ucfirst($page);
				$class = $page . "_Controller_" . $page;
				/**
				 * instantiante the appropriate class
				 */
				if(class_exists($class)){
					$controller = new $class;
				}
				else {
					//throw new Exception("Class '$class' not found!");
					echo '<meta http-equiv="Refresh" content="0;url='.SERVER_ROOT.'?index">';
				}
				
			}
			else {
				//throw new Exception("File '$page' not found!");
				echo '<meta http-equiv="Refresh" content="0;url='.SERVER_ROOT.'?index">';
			}
			
		}
		
		catch(Exception $e){
			echo $e->getMessage();
		}
		
		/**
		 * once we have the controller instantiated, executed the default function
		 * pass any GET variables to the main method
		 */
		$controller->main($getVars, $postVars);
	}
	
	public function otherClassLoader($className){
		
		/**
		 * Parse out filename where class should be located.
		 * This supports names like 'Example_Model' as well as 'Example_Model'.
		 */
		
		list($fileToLoad, $functionalityAndFolder) = preg_split("/_/", strrev($className), 2);
		$functionalityAndFolder = strrev($functionalityAndFolder);
		$fileToLoad = strrev($fileToLoad);
		list($trier, $functionality) = preg_split("/_/", strrev($functionalityAndFolder));
		$functionality = strrev($functionality);
		$trier = strrev($trier);
		
		/**
		 * select the folder where class should be located
		 * bassed on $trier
		 */
		switch(strtolower($trier)){
			case "model":
				$folder = "/".basename(models)."/";
				break;
			case "data":
				$folder = "/".basename(data)."/";
				break;
			case "library":
				$folder = "/".basename(libraries)."/";
				break;
			case "driver":
				$folder = "/".basename(libraries)."/".basename(drivers)."/";
				break;
			default:
				echo '<meta http-equiv="Refresh" content="0;url='.SERVER_ROOT.'?index">';
		}
		
		/**
		 * compose the filename
		 */
		$file = "./" . strtolower($functionality)  . strtolower($folder) . strtolower($fileToLoad) . ".php";
		try {
			
			/**
			 * fetch file
			 */
			if(file_exists($file)){
				/**
				 * get file
				 */
				include_once "$file";
			}
			else {
				/**
				 * file does not exists!
				 */
				//throw new Exception("File '$file' not found!");
				echo '<meta http-equiv="Refresh" content="0;url='.SERVER_ROOT.'?index">';
			}
		}
		
		catch(Exception $e){
			echo $e->getMessage();
		}
	}
}